package cn.edu.gzhu.workOrderManagement.controller;

import cn.edu.gzhu.workOrderManagement.constants.MessageConstant;
import cn.edu.gzhu.workOrderManagement.enumeration.UserAuthorityEnum;
import cn.edu.gzhu.workOrderManagement.pojo.dto.user.*;
import cn.edu.gzhu.workOrderManagement.pojo.vo.UserLoginVo;
import cn.edu.gzhu.workOrderManagement.pojo.vo.UserVo;
import cn.edu.gzhu.workOrderManagement.properties.JwtProperties;
import cn.edu.gzhu.workOrderManagement.result.Result;
import cn.edu.gzhu.workOrderManagement.service.UserService;
import cn.edu.gzhu.workOrderManagement.util.JwtUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.jsonwebtoken.Claims;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 普通用户的接口类
 * @author chen xi
 */

@RestController
@RequestMapping("/user")
@Tag(name = "用户管理接口")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;
    /**
     * 注册接口
     * @param userRegisterDto 包含username password authority
     * @return
     */
    @Operation(summary = "注册")
    @PostMapping("/register")
    public Result register(@RequestBody @Validated UserRegisterDto userRegisterDto){

        Boolean isSuccess = userService.saveUser(userRegisterDto);
        if(!isSuccess){
            return Result.error(MessageConstant.USERNAME_ALREADY_EXIT_OR_ORGANIZATION_NAME_ERROR);
        }
        return Result.success();
    }

    /**
     * 登入接口
     * @param userLoginDtoDto 包含username password authority
     * @return
     */
    @Operation(summary = "登入")
    @GetMapping("/login")
    public Result<UserLoginVo> login(@Validated UserLoginDto userLoginDtoDto){
        UserLoginVo userLoginVo = userService.getUserByUsernameAndPW(userLoginDtoDto);
        if (userLoginVo==null){
            return Result.error(MessageConstant.USERNAME_OR_PASSWORD_ERROR_OR_UNAVAILABLE);
        }
        return Result.success(userLoginVo);
    }

    /**
     * 组织负责人和管理员查询用户使用的接口，用拦截器鉴权
     * 组织负责人可按照用户名查找单个用户、或直接列出本企业所有用户
     * 管理员可按照组织名查询、按照权限查询、按照用户名查询
     * @return
     */
    @Operation(summary = "按条件查找用户")
    @GetMapping("/list")
    public Result<IPage<UserVo>> listUsers(@RequestHeader String token, @Validated UserListDto userListDto){

        IPage<UserVo> voPage = userService.listByConditions(userListDto);

        return Result.success(voPage);
    }

    /**
     * 组织负责人和管理员使用的接口
     * 组织负责人可以修改组织内用户的密码和启用状态
     * 管理员可以修改任意用户的权限和密码和启用状态和所属组织
     * @param userUpdateDto 包含:username,password,authority,isAvailable,organization
     * @return
     */
    @Operation(summary = "修改用户属性")
    @PostMapping("/update")
    public Result updateUser(@RequestHeader String token,@RequestBody @Validated UserUpdateDto userUpdateDto){
        Claims claims = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(), token);
        UserAuthorityEnum authority =UserAuthorityEnum.value2Object( claims.get("authority").toString());
        Boolean isSuccess =  userService.updateUser(authority,userUpdateDto);
        if (!isSuccess){
            return Result.error(MessageConstant.PARAMETER_ERROR);
        }
        return Result.success();
    }

    /**
     * 组织负责人和管理员用来删除用户的接口，可批量也可单个
     * @param token
     * @param userDeleteDto 只有List usernames
     * @return
     */

    @Operation(summary = "根据用户名删除用户")
    @PostMapping("/delete")
    public Result deleteUser(@RequestHeader String token, @RequestBody @Validated UserDeleteDto userDeleteDto){

        Boolean isSuccess =  userService.deleteUsersByUsernames(userDeleteDto);
        if (!isSuccess){
            return Result.error(MessageConstant.PARAMETER_ERROR);
        }
        return Result.success();
    }

    @Operation(summary = "批量激活用户")
    @PostMapping("/activate")
    public Result activateUsers(@RequestHeader String token, @RequestBody @Validated UserDeleteDto userDeleteDto ){
        Boolean isSuccess = userService.activateUsers(userDeleteDto);
        if (!isSuccess){
            return Result.error(MessageConstant.PARAMETER_ERROR);
        }
        return Result.success();
    }


}
